//
//  StudyInforListVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/2/6.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "StudyInforListVC.h"
#import "studyTopTitleCell.h"
#import "studyListCollectionCell.h"
#import "StageModel.h"
#import "StudyInforVC.h"

@interface StudyInforListVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *top_collectionView;
@property (strong, nonatomic) IBOutlet UICollectionView *bottom_collectionView;

@end

@implementation StudyInforListVC{
    NSArray *titleArr;//头部内容数组
    NSMutableArray *nameArr;
    NSInteger page_index;//页码
    NSInteger choosetag;//头部被选中的tag值
    NSMutableDictionary *chooseDic;
    RequestServices *service;
    NSMutableArray *list_arr;//列表数组
    NSString *passID;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.name;
    [self.backKuang setHidden:YES];
    [self showCollectionView];
    service = [[RequestServices alloc]initWithDelegate:self];
    [service getStageList:self.id tag:8010];
    
    __weak __typeof(self)weakSelf = self;
    //左划
    self.bottom_collectionView.mj_trailer =[MJRefreshTrailer trailerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    self.bottom_collectionView.mj_trailer.hidden = YES;
}

- (void)headerRefresh
{
    service.view = self.view;
    page_index = 1;
    [service getCurriculumList:[NSString stringWithFormat:@"%ld",page_index] categoryId:self.id recommend:@"0" stageId:passID tag:8000];
}

- (void)footerRefresh
{
    page_index++;
    [service getCurriculumList:[NSString stringWithFormat:@"%ld",page_index]categoryId:@"0" recommend:@"0" stageId:passID tag:8001];
}

    //添加视图
-(void)showCollectionView{
    self.top_collectionView.delegate = self.bottom_collectionView.delegate = self;
    self.top_collectionView.dataSource = self.bottom_collectionView.dataSource = self;
    
    [self.top_collectionView registerNib:[UINib nibWithNibName:@"studyTopTitleCell" bundle:nil] forCellWithReuseIdentifier:@"studyTopTitleCell"];
    [self.bottom_collectionView registerNib:[UINib nibWithNibName:@"studyListCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"studyListCollectionCell"];
    chooseDic = [NSMutableDictionary dictionary];
    nameArr = [[NSMutableArray alloc]init];
   
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    if (tag == 8010) {
        titleArr = [StageModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        for (NSInteger i = 0 ; i<titleArr.count; i++) {
            StageModel *model = titleArr[i];
            [nameArr addObject:model.stageName];
        }
        [self.top_collectionView reloadData];
        [self headerRefresh];

    }
    if (tag == 8000 || tag == 8001) {
        [self.bottom_collectionView.mj_trailer endRefreshing];
        
        [self HideerrorView];
        [self HideNoDataView];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
        if (tag == 8000) {
            if ([tempArr CheckNSArrayData]) {
                list_arr = [CurriculumListModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
                
            }
            if (tempArr.count == 0) {
                [self ShowNoDataView:self.bottom_collectionView Orgin:0];
            }
        }else {
            [list_arr addObjectsFromArray:[CurriculumListModel mj_objectArrayWithKeyValuesArray:tempArr]];
        }
        
        [self.bottom_collectionView reloadData];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self ShowNewMsg:error.domain];
    [self.bottom_collectionView.mj_trailer endRefreshing];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.top_collectionView) {
        return nameArr.count;
    }else{
        return list_arr.count;
    }
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.top_collectionView) {
//        CGFloat width = [[NSString stringWithFormat:@"%@",nameArr[indexPath.item]] boundingRectWithSize:CGSizeMake(0, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : AutoLabFont2} context:nil].size.width;
        return CGSizeMake(collectionView.frame.size.width*0.15, collectionView.frame.size.height*0.4);
    }else {
        return CGSizeMake((collectionView.frame.size.width - 40)/2,collectionView.frame.size.height*0.7);
    }
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.top_collectionView) {
        studyTopTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"studyTopTitleCell" forIndexPath:indexPath];
        cell.title_lab.text = [NSString stringWithFormat:@"%@",nameArr[indexPath.row]];
        
        //多选
        //        if ([[chooseDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.row]] isEqualToString:[NSString stringWithFormat:@"%ld",indexPath.row]] ) {
        //            [cell.title_lab setBackgroundColor:[UIColor orangeColor]];
        //            cell.title_lab.textColor = [UIColor whiteColor];
        //        }else{
        //            [cell.title_lab setBackgroundColor:[UIColor whiteColor]];
        //            cell.title_lab.textColor = [UIColor blackColor];
        //
        //        }
        
        //单选
        if (choosetag == indexPath.item) {
            cell.back_img.image = [UIImage imageNamed:@"study_choose"];
        }else{
            cell.back_img.image = [UIImage imageNamed:@""];

        }
        return cell;
    }else{
        studyListCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"studyListCollectionCell" forIndexPath:indexPath];
        cell.model = list_arr[indexPath.item];
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.top_collectionView) {
        //单选
        
        choosetag = indexPath.row;
        
        //多选
        //        if (![[chooseDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.row]] isEqualToString:[NSString stringWithFormat:@"%ld",indexPath.row]] ) {
        //            [chooseDic setValue:[NSString stringWithFormat:@"%ld",indexPath.row] forKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        //        }else{
        //            [chooseDic removeObjectForKey:[NSString stringWithFormat:@"%ld",indexPath.row]];
        
        StageModel *model = titleArr[indexPath.item];
        passID = [NSString stringWithFormat:@"%@",model.id];
        [self headerRefresh];
        [self.top_collectionView reloadData];
    }
    
    if (collectionView == self.bottom_collectionView) {
        StudyInforVC *vc = [[StudyInforVC alloc]init];
        CurriculumListModel *model = list_arr[indexPath.item];
        vc.courseId = model.categoryId;
        vc.name = [NSString stringWithFormat:@"学习中心-%@",model.categoryName];
        vc.passDic = [model mj_keyValues];
        if ([model.coursePayType intValue] == 1) {
            [self ShowNewMsg:@"付费才可进入"];
            return;
        }
        [self PushToViewController:vc];
    }
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView
    layout:(UICollectionViewLayout *)collectionViewLayout
    insetForSectionAtIndex:(NSInteger)section
{
    if (collectionView == self.top_collectionView) {
        return UIEdgeInsetsMake(self.top_collectionView.frame.size.height*0.6, 0.0f, 0.0f, 0.0f);
    }else{
        return UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
